function UpTradePwd() {
    this.init();
}
UpTradePwd.prototype.init = function() {
	var me = this;
	var template = __inline("./uptradepwd.tmpl");
    var content = template({
    	current:me._current,
    	count:me._count
    });
    $(document.body).append(content);
    this.eventCenter();
};
UpTradePwd.prototype.show = function() {
    var me = this;
	$('#uptradepwd').modalBox({
        keyClose:true,
        overlay:true,
        bodyClose:false
    });
}
UpTradePwd.prototype.hide = function() {
    $('#uptradepwd').modalBox('close');
}
UpTradePwd.prototype.eventCenter = function () {
    var me = this;
    $('.vc_button').bind('click',{'me':me},me.sendVC);
    $('.close_btn').click(function () {
        $('#uptradepwd').modalBox('close');
    });
    $('.input_text').click(function(event) {
        $(event.target).find('input').focus();
    })
    $('.form_el input').change(function() {
        me.checkForm(event.target);
    });
    $('#uptradepwd_submit').click(function() {
        var input_list = $('.form_el input');
        for(var i=0; i < input_list.length; i++) {
            var one_el = input_list[i];
            var status = me.checkForm(one_el);
            if(!status) {
                break;
            }
        }
        me.submitForm();
    })
}
UpTradePwd.prototype.sendVC = function(event) {
    var type = 0;
    if(event.data && event.data.type) {
        type = event.data.type;
    }
    $('.get_vc').text(60);
    $('.get_vc').unbind('click');
    $.post('/account/sendmyidentitycode',{'type':type},function(data) {
        event.data.me.clearMessage($('#up_vc'));
        if(data && data.error_no == 0) {
            var setTime = setInterval(function(){
                var time = $('.get_vc').text();
                    if(time == 1) {
                        $('.get_vc').text('获取');
                        $('.get_vc').removeClass('disabled');
                        $('.get_vc').bind('click',event.data.me.sendVC);
                        clearInterval(setTime);
                        event.data.me.setMessage($('#up_vc'), '如果1分钟内没有收到校验短信，您可以通过<a id="get_voice_vc">语音方式</a>获取验证码，此服务免费。','info_msg');
                        $('#get_voice_vc').css('cursor','pointer').css('color','#F93').css('text-decoration','underline').css('padding','0px 10px');
                        $('#get_voice_vc').bind('click',{'me':event.data.me, 'type':1},event.data.me.sendVC);
                    } else {
                        $('.get_vc').text(parseInt(time) -1);
                    }
            },1000);
        } else {
            event.data.me.setMessage($('#up_vc'),data.error_message);
        }
    },'json');
};
UpTradePwd.prototype.checkForm=function(html) {
    var me = this;
    var id = $(html).attr('id');
    var jId = '#' + id;
    me.clearMessage($(jId));
    if(id == 'up_vc') {
        if($(jId).val().length < 6) {
            me.setMessage($(jId), '请输入6位验证码。');
            return false;
        }
    } else if(id == 'up_trade_passwd' || id=='up_re_trade_passwd') {
        if($(jId).val().length < 6 || $(jId).val().length > 16) {
            me.setMessage($(jId),'交易密码长度需要6~16位。');
            return false;
        }
        if(id == 'up_re_trade_passwd' && $('#up_trade_passwd').val()!= $('#up_re_trade_passwd').val())  {
            me.setMessage($(jId),'2次密码不一致。');
            return false;
        }
    }
    return true;
};
UpTradePwd.prototype.clearMessage =function($container) {
    $container.parent().parent().find('.input_info').text('');
};
UpTradePwd.prototype.submitForm = function() {
    var me = this;
    var vc = $('#up_vc').val();
    var trade_passwd = $('#up_trade_passwd').val();
    $.post('/account/modifytradepasswd',{'code':vc, 'trade_passwd':trade_passwd} ,function(data) {
        if(data && data.error_no == 0) {
            alert('交易密码设置成功');
            location.reload();
        } else {
            if(data.error_no == 10001) {
                me.setMessage($('#up_trade_passwd'),data.error_message);
            } else if(data.error_no == 10203 || data.error_no == 10204) {
                me.setMessage($('#up_vc'),data.error_message);
            } else {
                alert(data.error_message);
            }

        }
    },'json');
};
UpTradePwd.prototype.setMessage = function($container,message,type) {
    $('.form_el .input_info').text('');
    type = type || 'error_msg';
    $infoContainer = $container.parent().parent().find('.input_info');
    if(message.length > 15) {
        $infoContainer.css('margin-top','0px');
    } else{
        $infoContainer.css('margin-top','20px');
    }
    $infoContainer.html(message);
    $infoContainer.removeClass('error_msg');
    $infoContainer.removeClass('info_msg');
    $infoContainer.addClass(type);
}
module.exports = UpTradePwd;